package com.homegoods.resource.pullDownMenuAdapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ListView;

import com.homegoods.resource.R;
import com.kedll.library.widget.pullDownMenu.BasePullDownMenuAdapter;
import com.kedll.library.widget.pullDownMenu.OnDisplayClickListener;

import java.util.List;

/**
 * @author QiQi
 * @date 2017/9/8
 */

public class SwitchConditionAdapter extends BasePullDownMenuAdapter {
    private CheckedTextAdapter adapter;
    private ListView listView;

    private OnItemClickListener onItemClickListener;

    public SwitchConditionAdapter(Context context, OnItemClickListener onItemClickListener) {
        super(context);
        this.onItemClickListener = onItemClickListener;
        adapter = new CheckedTextAdapter(context);
    }

    @Override
    public View getView(final OnDisplayClickListener itemClickListener) {
        if (listView == null) {
            listView = new ListView(mContext);
            listView.setDivider(ContextCompat.getDrawable(mContext, R.drawable.drawable_line));
            listView.setDividerHeight(1);
            listView.setAdapter(adapter);

            listView.setOnItemClickListener((parent, view, position, id) -> {
                itemClickListener.onCollapse(attachView);
                if (position == adapter.getSelectionIndex()) {
                    return;
                }
                adapter.setSelectionIndex(position);
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(position);
                }
            });
        }
        return listView;
    }

    public void setData(List data) {
        adapter.clear();
        adapter.addAll(data);
        adapter.notifyDataSetChanged();
    }


    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}
